package com.tools.web.work.excel.poi;

import com.tools.common.object.Note;

import java.util.Arrays;
import java.util.Objects;

/**
 * 内部类: 存储表头数据的表头项结点实例
 * */
@Note("内部类: 存储表头数据的表头项结点实例")
public final class ExcelField {

    @Note("表头名称")
    private String[] names;

    @Note("表头数据对应数据实例对象的属性名称")
    private String field;

    @Note("表头单元格的宽度，即 Excel 表的列宽")
    private int headWidth = DEFAULT_HEAD_WIDTH;

    @Note("多行相邻的值相同时是否合并单元格，默认为 false")
    private boolean sameValueMerge = false;

    @Note("全局默认的表头单元格宽度")
    public static final int DEFAULT_HEAD_WIDTH = 20;

    public ExcelField(boolean sameValueMerge, String field, String... names) {
        this.setNames(names);
        this.setField(field);
        this.setSameValueMerge(sameValueMerge);
    }

    public ExcelField(int headWidth, boolean sameValueMerge, String field, String... names) {
        this(sameValueMerge, field, names);
        this.setHeadWidth(headWidth);
    }

    public String[] getNames() {
        return names;
    }

    public void setNames(String[] names) {
        this.names = (names == null || names.length == 0) ? new String[] {""} : names;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = (field == null) ? "" : field;
    }

    public int getHeadWidth() {
        return headWidth;
    }

    public void setHeadWidth(int headWidth) {
        if(headWidth > 1) this.headWidth = headWidth;
    }

    public boolean isSameValueMerge() {
        return sameValueMerge;
    }

    public void setSameValueMerge(boolean sameValueMerge) {
        this.sameValueMerge = sameValueMerge;
    }

    @Override
    public String toString() {
        return "ExcelField{" +
                "field='" + field + '\'' +
                ", headWidth=" + headWidth +
                ", sameValueMerge=" + sameValueMerge +
                ",names=" + Arrays.toString(names) +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ExcelField that = (ExcelField) o;
        return Arrays.equals(names, that.names) && Objects.equals(field, that.field);
    }

    @Override
    public int hashCode() {
        return Objects.hash(names, field);
    }
}
